package com.hgwz;

import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Play {
    public static void main(String[] args) {
        System.out.println("开始打斗游戏！");

        //创建默认英雄
        List<Hero> heroes = new ArrayList<>();
        heroes.add(new Hero("1001", "小兵", 30, 1));
        heroes.add(new Hero("1002", "中将", 300, 10));
        heroes.add(new Hero("1003", "肉盾", 500, 30));
        heroes.add(new Hero("1004", "射手", 50, 3));

        //主界面
        while (true) {
            System.out.println("------ 欢迎来到回合制游戏 ------");
            System.out.println("1. 创建英雄");
            System.out.println("2. 查看英雄信息");
            System.out.println("3. 查看血量小于100的英雄名字");
            System.out.println("4. 打斗");
            System.out.println("5. 修改英雄信息");
            System.out.println("6. 删除英雄");
            System.out.println("7. 退出系统");
            System.out.println("请输入你的选项：");

            Scanner scanner = new Scanner(System.in);
            String choiceS = scanner.nextLine();
            int choiceI = Integer.parseInt(choiceS);

            switch (choiceI) {
                case 1:
                    System.out.println("1. 创建英雄功能实现");
                    addHero(heroes);
                    break;
                case 2:
                    System.out.println("2. 查看英雄信息功能实现");
                    viewAllHeros(heroes);
                    break;
                case 3:
                    System.out.println("3. 查看血量小于100的英雄名字功能实现");
                    viewLess100(heroes);
                    break;
                case 4:
                    System.out.println("4. 打斗功能实现");
                    fight(heroes);
                    break;
                case 5:
                    System.out.println("5. 修改英雄信息功能实现");
                    updateHero(heroes);
                    break;
                case 6:
                    System.out.println("6. 删除英雄功能实现");
                    delHero(heroes);
                    break;
                case 7:
                    System.out.println("7. 退出系统功能实现");
                    System.exit(0);
            }
        }
    }

    //1. 创建英雄功能
    private static void addHero(List<Hero> heroes) {
        System.out.println("当前英雄：");
        System.out.println(heroes);
        System.out.println("\n== 添加新英雄 ==");
        Scanner scanner = new Scanner(System.in);

        //输入英雄ID， 判断是否已存在
        String id;
        while (true) {
            System.out.println("请输入英雄ID：");
            id = scanner.nextLine();
            //检查ID是否已经存在
            if (!isExist(heroes, id)) {
                break;
            }else{
                System.out.println("ID = " + id + "的英雄已经存在, 请重新输入英雄编号！");
            }
        }

        System.out.println("请输入英雄姓名：");
        String name = scanner.nextLine();

        System.out.println("请输入英雄血量：");
        int hp = scanner.nextInt();

        System.out.println("请输入英雄攻击力：");
        int power = scanner.nextInt();

        Hero newHero = new Hero(id, name, hp, power);
        heroes.add(newHero);
        System.out.println("新建英雄成功！！！");
    }

    private static boolean isExist(List<Hero> heroes, String id) {
        for (Hero hero : heroes) {
            if (hero.getId().equalsIgnoreCase(id)) {
                return true;
            }
        }
        return false;
    }

    // 2. 查看全部英雄信息
    private static void viewAllHeros(List<Hero> heros) {
        System.out.println(heros);
    }

    // 3. 查看血量少于100的英雄名字
    private static void viewLess100(List<Hero> heroes) {
        List<String> names = heroes
                .stream()
                .filter(hero -> hero.getHp() < 100)
//                .sorted((h1, h2) -> Integer.compare(h2.getPower(), h1.getPower()))
                .map(Hero::getName)
                .collect(Collectors.toList());
        System.out.println(names);
    }

    private static void fight(List<Hero> heroes) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入我方出战英雄ID:");
        String mId = scanner.nextLine();
        System.out.println("请输入敌方出战英雄ID:");
        String enemyId = scanner.nextLine();

        if(!isExist(heroes, mId) || !isExist(heroes, enemyId)){
            System.out.println("我方或者敌方ID不存在！！ \n");
            return;
        }

        Hero myHero = null;
        for (Hero hero : heroes) {
            if (hero.getId().equalsIgnoreCase(mId)) {
                myHero = hero;
                break;
            }
        }

        Hero enemyHero = null;
        for (Hero hero : heroes) {
            if (hero.getId().equalsIgnoreCase(enemyId)) {
                enemyHero = hero;
            }
        }

        System.out.println("开始战斗！");
        int i = 0;
        String re = "";
        while (true) {
            //记录回合数
            i++;

            // 算法： 剩余血量 = 自己血量-对方攻击力
            int myHP = myHero.getHp() - enemyHero.getPower();
            myHero.setHp(myHP);
            int enemyHP = enemyHero.getHp() - myHero.getPower();
            enemyHero.setHp(enemyHP);
            System.out.println("我 [" + myHero.getName() + "] 当前血量：" + myHero.getHp());
            System.out.println("敌方 [" + enemyHero.getName() + "] 当前血量：" + enemyHero.getHp());

            //游戏结束条件：任意一方血量 <= 0
            if (myHero.getHp() <= 0 || enemyHero.getHp() <= 0) {
                int result = myHero.getHp() - enemyHero.getHp();
                if (result > 0) {
                    //我方获胜
                    re = "我方：" + myHero.getName() + " 赢了";
                } else if (result == 0) {
                    //平局
                    re = myHero.getName() + "与" + enemyHero.getName() + "平局";
                } else {
                    //敌方获胜
                    re = "敌方：" + enemyHero.getName() + " 赢了";
                }
                break;
            }
        }
        System.out.println("最终战斗结果： 一共战斗" + i + "回合，结果为 ~ " + re);
    }

    //5. 修改英雄信
    private static void updateHero(List<Hero> heroes) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入需要修改的英雄ID");
        String id = scanner.nextLine();
        if (!isExist(heroes, id)) {
            System.out.println("ID为" + id + "不已存在，需要输入已有英雄ID！");
            return;
        }

        System.out.println("请输入英雄姓名：");
        String name = scanner.nextLine();
        System.out.println("请输入英雄血量：");
        int hp = scanner.nextInt();
        System.out.println("请输入英雄攻击力：");
        int power = scanner.nextInt();

        Hero updateHero = new Hero(id, name, hp, power);

        //更新对应英雄信息
        for(int i = 0; i < heroes.size(); i++){
            Hero hero = heroes.get(i);
            if (hero.getId().equalsIgnoreCase(id)){
                heroes.set(i, updateHero);
                break;
            }
        }
        System.out.println("英雄信息更新成功！");
    }


    //6. 删除英雄
    private static void delHero(List<Hero> heroes){
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入需要删除的英雄ID: ");
        String id = scanner.nextLine();
        if (!isExist(heroes, id)) {
            System.out.println("ID为" + id + "不已存在，需要输入已有英雄ID！");
            return;
        }

        for(Hero hero: heroes){
            if(hero.getId().equals(id)){
                //删除该英雄
                heroes.remove(hero);
                break;
            }
        }
        System.out.println("ID为" + id + "已被删除！");
    }
}